package com.nfc2me;

import java.util.ArrayList;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.nfc.NfcAdapter;
import android.nfc.tech.MifareClassic;
import android.nfc.tech.Ndef;
import android.os.Bundle;

import android.util.Log;
import android.view.Gravity;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;


public class N2M_FindCar extends Activity  {
	/** Called when the activity is first created. */
	private NfcAdapter mAdapter;
	private PendingIntent mPendingIntent;
	private IntentFilter[] mFilters;
	private String[][] mTechLists;
	private SimpleView sv=null;
	private int index=0;
	private FrameLayout lay;
	private ArrayList<Coordinate> pList;
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		pList=new ArrayList<Coordinate>() ;
		Intent mIntent = getIntent();
		String dataString = mIntent.getDataString();
		Coordinate p1= new Coordinate();
		p1.setX(200);
		p1.setY(400);
		
		Coordinate p2=new Coordinate();
		p2.setX(100);
		p2.setY(150);
		pList.add(p1);
		pList.add(p2);

		// the n2m tag is read,should do the following things
		// 1.check the data string is in right regular expression
		// 2.check whether the data is read before in db, and the relative park
		// image and tag position info is stored in the local
		// if not, get the park image and tag position info from the server
		// 3. show the park image and the tag
		if (dataString != null) {
			//Log.d("n2m tag:", dataString);
			index = Integer.parseInt(dataString.substring(dataString
					.lastIndexOf("/")+1));
		}
		
		//View back= (View)findViewById(R.id.view1);
		lay=new FrameLayout(this);
		lay.setBackgroundResource(R.drawable.back);
		
		sv=new SimpleView(this);
		//sv.setBackgroundResource(R.drawable.back);
		Log.d("N2M:",lay.toString());
		
		sv.setX(index);
		sv.setY(index);
		lay.addView(sv);
		setContentView(lay);
		//lay.addView(back);
        FrameLayout.LayoutParams params=(FrameLayout.LayoutParams)sv.getLayoutParams();
        params.leftMargin=50;
        params.topMargin=100;
        params.gravity=Gravity.LEFT;
        params.height=LayoutParams.WRAP_CONTENT;
        params.width=LayoutParams.WRAP_CONTENT;
        sv.setLayoutParams(params);
		lay.invalidate();
		

		
		// data string like: n2m://nfc2me.com/p/x, x means the Xth tag
		mAdapter = NfcAdapter.getDefaultAdapter(this);

		// Create a generic PendingIntent that will be deliver to this activity.
		// The NFC stack
		// will fill in the intent with the details of the discovered tag before
		// delivering to
		// this activity.
		mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
				getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), PendingIntent.FLAG_CANCEL_CURRENT);

		// Setup an intent filter for all MIME based dispatches
		IntentFilter nfc_tech = new IntentFilter(
				NfcAdapter.ACTION_NDEF_DISCOVERED);
		try {
			nfc_tech.addDataScheme("n2m");
		} catch (Exception e) {
			throw new RuntimeException("fail", e);
		}
		mFilters = new IntentFilter[] { nfc_tech, };

		// Setup a tech list for all NfcF tags
		mTechLists = new String[][] {
				new String[] { MifareClassic.class.getName() },
				new String[] { Ndef.class.getName() } };
	}

	@Override
	public void onResume() {
		super.onResume();
		mAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters,
				mTechLists);
	}

	@Override
	public void onNewIntent(Intent intent) {
		//Log.d("Foreground dispatch", "Discovered tag with intent: " + intent);
		// mText.setText("Discovered tag " + ++mCount + " with intent: " +
		// intent);
		//Log.d("data in the new intent", intent.getDataString());
		String dataString = intent.getDataString();
		if (dataString != null) {
			
			index = Integer.parseInt(dataString.substring(dataString
					.lastIndexOf("/")+1));
			Log.d("n2m onNewIntent index :", index+"");
	        FrameLayout.LayoutParams params=(FrameLayout.LayoutParams)sv.getLayoutParams();
	        params.leftMargin=pList.get(index-1).getX();
	        params.topMargin=pList.get(index-1).getY();
	        sv.setLayoutParams(params);
			lay.invalidate();
			
		}
	}

	@Override
	public void onPause() {
		super.onPause();
		mAdapter.disableForegroundDispatch(this);
		// throw new RuntimeException("onPause not implemented to fix build");
	}


}
